<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET'){
	
	$forms = $_GET;
	$type = $_GET["type"];
	$shop_name = $_GET["shop_name"];
	$style = $_GET["style"];
	$current=1;
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}
	
	$where = '';
	$query = '';
	
	if($type != null){
		$where = $where." and type like '%".$type."%'";
		$query .="&type=".$type;
	}
	if($shop_name != null){
		$where = $where." and shop_name like '%".$shop_name."%'";
		$query .="&shop_name=".$shop_name;
	}
	/*
	if($style != null){
		$where = $where." and (style like '%".$style."%'";
		$where = $where." or CONCAT( style1,  'mm×', style2 ,'m' ) = '".$style."') ";
		$query .="&style=".$style;
	}
	*/
	$sa = split("\*|×", $style);
	if(count($sa)>=2){
		$sa[0] = str_replace("mm","",$sa[0]);
		$sa[1] = str_replace("m","",$sa[1]);
	}else{
		$sa[0] = str_replace("mm","",$sa[0]);
		$sa[0] = str_replace("m","",$sa[0]);
	}
	
	if($style != null){
		$where = $where." and (style like '%".$style."%'";
		if(count($sa)>=2){
			$where = $where." or (style1 = '".$sa[0]."' ";
			$where = $where." and style2 = '".$sa[1]."' )) ";
		}else{
			$where = $where." or style1 = '".$sa[0]."' ";
			$where = $where." or style2 = '".$sa[0]."' ) ";
		}
		$query .="&style=".$style;
	}
	
	$sql =  "SELECT * FROM p_shop WHERE 1 = 1 $where order by u_date desc LIMIT $st, ".PAGE_SIZE;
	$sqlcount ="SELECT COUNT(*) as cnt FROM p_shop WHERE 1=1 ".$where;
	$db = new Class_DB;
	$res = $db->query($sql);
	
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		/*
		$sarray = unserialize($row['style']);
		if(count($sarray)>1){
			$row['style'] = $sarray[0]."mm×".$sarray[1]."m";
		}else{
			$row['style'] = $sarray[0];
		}
		*/
		
		if($row['style']==''){
			$row['style'] = $row['style1']."mm×".$row['style2'].'m';
		}
		$shoplist[] = $row;
	}
	
	$cnt = 0;
	$db->query( $sqlcount );
	$row = $db->fetch();
	$cnt = $row['cnt'];
	
	$smarty->assign('page', getPage($current, $cnt,$query));
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
	
	/******/
}

$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->assign('shoplist', $shoplist);

$smarty->assign('cnt', $cnt);

$smarty->display('web-admin/shop_search.html');


?>